<template>
	<view class="boxs">
		<view class="y-title">
			登录
		</view>
		<u-divider></u-divider>
		<u--form labelPosition="left">

			<u-form-item label="账号" labelWidth="60" borderBottom ref="item1">
				<u--input border="none" placeholder="请输入账号" v-model="phone"></u--input>
			</u-form-item>


			<u-form-item label="验证码" :labelWidth="60" borderBottom>
				<u--input border="none" v-model="sms" placeholder="请填写验证码"></u--input>
				<u-button slot="right" :text="tips" type="success" size="mini" @click="getMessage"
					:disabled="phone.length!=11 || isClick"></u-button>
			</u-form-item>
		</u--form>

		<u-button type="success" @click="logins" block>登录</u-button>



	</view>
</template>

<script>
	import {
		bindUser,
		getSendMessage
	} from "../../api/login.js"
	import {
		v4
	} from "uuid"
	export default {
		data() {
			return {
				tips: "获取验证码",
				sms: "",
				phone: "16651737795",
				isClick: false,
				dtime: 60,
				timeData: {},
			}
		},
		onLoad() {

		},
		methods: {
			//开始
			start() {
				this.$refs.countDown.start();
			},
			// 暂停
			pause() {
				this.$refs.countDown.pause();
			},
			// 重置
			reset() {
				this.$refs.countDown.reset();
			},
			onChange(e) {
				this.timeData = e
			},



			//点击发送验证码 倒计时
			daojishi() {
				if (this.dtime <= 1) {
					this.dtime = 60;
					this.isClick = false;
					this.tips = "获取验证码"
					return
				}
				this.dtime -= 1;
				this.tips = this.dtime + "秒后重新获取";
				setTimeout(() => {
					this.daojishi();
				}, 1000);
			},
			//发送验证码
			getMessage() {
				uni.showLoading({
					title: "请稍后",
				})
				getSendMessage({
					account: this.phone
				}).then((res) => {
					uni.hideLoading();
					if (res.result == -1) {
						uni.showToast({
							title: "发送失败,请前往数据库查看登录码",
							icon: "none"
						});
						this.isClick = true;
						this.daojishi();
					} else {
						uni.showToast({
							title: "发送失败",
							icon: "error"
						})
					}
				})
			},

			//绑定账号/绑定用户
			logins() {
				uni.showLoading({
					title: "登陆中",
					icon: "none"
				})

				const uuid = v4().replaceAll("-", "").toUpperCase();
				uni.setStorageSync("appid", uuid);

				bindUser({
					sms: this.sms,
					phone: this.phone,
					openid: uni.getStorageSync("appid"),
				}).then((res) => {
					uni.hideLoading();
					if (res.result === 0) {
						uni.reLaunch({
							url: "/pages/index/index"
						})
					} else {
						uni.showToast({
							title: "登录失败",
							icon: "error"
						})
					}

				})

			},

		}
	}
</script>

<style>
	@import url("/static/main.css");
	@import url("/static/iconfont/icons.css");

	.boxs {
		padding: 0;
		margin: 0;
		box-sizing: border-box;
		padding: 5px;
	}

	.y-title {
		margin-top: 20rpx;
		font-size: 36rpx;
		font-weight: 600;
		text-align: center;
	}
</style>